package com.gxa.eshop.portal.controller;

import com.gxa.eshop.core.exception.BusinessException;
import com.gxa.eshop.core.pojo.Goods;
import com.gxa.eshop.core.utils.JsonResult;
import com.gxa.eshop.portal.service.GoodsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    GoodsService goodsService;

    @PostMapping("/getAll")
    public JsonResult getAll(@RequestParam(value = "field",required = false) String field){
        return null;
    }

    @PostMapping("/goodsDetail")
        public JsonResult goodsDetail(@RequestParam(value = "id") Long id) throws BusinessException {

            Goods goods = goodsService.queryGoodsByCacheId(id);
            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode("1");
            jsonResult.setData(goods);
        return jsonResult;
    }

    @PostMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id") Long id) throws BusinessException {

        Boolean aBoolean = goodsService.removeGoodsCache(id);
        JsonResult jsonResult = new JsonResult();
        if(aBoolean){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }
        return jsonResult;
    }

    @PostMapping("/seckill")
    public JsonResult seckill(@RequestParam(value = "id") Long id) {

        Boolean seckill = goodsService.seckill(id);
        JsonResult jsonResult = new JsonResult();
        if(seckill){
            jsonResult.setCode("1");
            jsonResult.setMsg("恭喜你,秒杀成功");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("很遗憾秒杀失败,下次再接再厉");
        }
        return jsonResult;
    }
}
